﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using OrderManagement;
using System.Collections.Generic;

namespace Test
{
    [TestClass]
    public class OrderSort //测试默认排序方法
    {
        public static List<Order> Creat()
        {
            Customers customers1 = new Customers("张小亮", "C3-224", "110");
            Customers customers2 = new Customers("渣渣诚", "C3-319", "119");
            Goods good1 = new Goods("康师傅泡面", 4.5, 12, 2); // 9
            Goods good2 = new Goods("泡面搭档火腿肠", 2, 12, 3); //6
            Goods good3 = new Goods("果粒橙", 4, 12, 1); //4
            Goods good4 = new Goods("营养快线", 5, 12, 2);//10
            List<Goods> goods1 = new List<Goods>();
            List<Goods> goods2 = new List<Goods>();
            List<Goods> goods3 = new List<Goods>();
            goods1.Add(good1);
            goods1.Add(good2);

            goods2.Add(good3);
            goods2.Add(good4);

            goods3.Add(good1);
            goods3.Add(good4);
            OrderDetails orderDetails1 = new OrderDetails(goods1);
            OrderDetails orderDetails2 = new OrderDetails(goods2);
            OrderDetails orderDetails3 = new OrderDetails(goods3);
            //生成测试订单
            Order order1 = new Order(customers1, orderDetails1, "000001");
            Order order2 = new Order(customers2, orderDetails2, "000002");
            Order order3 = new Order(customers1, orderDetails3, "000003");
            List<Order> orders = new List<Order>();
            orders.Add(order3);
            orders.Add(order2);
            orders.Add(order1);
            return orders;

        }
        public static bool Judge1(List<Order> list)
        {
            bool flag = true;
            for (int i = 0; i < list.Count - 1; i++)
            {
                if (list[i].OrderDetails.Price > list[i + 1].OrderDetails.Price)
                {
                    flag = false;
                }
            }
            return flag;
        }
        public static bool Judge(List<Order> list)
        {
            bool flag = true;
            for(int i = 0; i < list.Count-1; i++)
            {
                if (Convert.ToInt32(list[i].ID)>Convert.ToInt32(list[i+1].ID))
                {
                    flag = false;
                }
            }
            return flag;
        }
        [TestMethod]
        public void TestMethod1()
        {
            List<Order> list = Creat();
            OrderService service = new OrderService(list);
            service.SortOrder();
            Assert.IsTrue(Judge(service.GetOrders()));
        }
        [TestMethod]
        public void TestMethod2()
        {
            List<Order> list = null;
            OrderService service = new OrderService(list);
            service.SortOrder();
            Assert.IsNull(service.GetOrders());
        }
        [TestMethod]
        public void TestMethod3()
        {
            List<Order> orders = new List<Order>();
            orders = Creat();
            OrderService service = new OrderService(orders);
            service.SortOrder((p1, p2) => p1.OrderDetails.Price.CompareTo(p2.OrderDetails.Price));
            Assert.IsTrue(Judge1(service.GetOrders()));
        }
    }
}
